<?php


namespace app\api\controller\v18;


use app\api\controller\v13\Auth2;
use app\common\cachemodel\MiniDateText;
use app\common\cachemodel\Recode;
use app\common\cachemodel\ZlAstroCalendarGoods;
use app\common\cachemodel\AstroChange as AstroChangeModel;

class AstroCalendar extends Auth2
{
    /**
     * 获取某月天象变动信息
     * @throws \Exception
     */
    public function getMonthChange()
    {
        $date = input('date/s', '');
        $timezone = input('timezone/f', 8);

        if (!$date)
        {
            return show(config('code.error'), '请求异常');
        }

        $astroChangeData = (new AstroChangeModel())->getChangeMonthList($date, $timezone);

        $planetsData = config('astrolog.planets_name');
        $signsData = config('astrolog.sign_name');
        $data = [];
        foreach ($astroChangeData as $item)
        {
            $type = 3;
            switch ($item['type'])
            {
                case 1:
                    if ((int)$item['pid'] === 0)
                    {
                        $type = 1; // 太阳换座
                    }
                    $text = explode('|', $planetsData[$item['pid']])[2] . explode('|', $signsData[$item['in_sign_id']])[2];
                    break;
                case 2:
                    $text = explode('|', $planetsData[$item['pid']])[2] . ((int)$item['speed'] === 1 ? '顺' : '逆');
                    break;
                case 3:
                    $text = '日食';
                    break;
                case 4:
                    $text = '月食';
                    break;
                case 5:
                    $type = 2; // 星月
                    $text = '新月';
                    break;
                case 6:
                    $text = '满月';
                    break;
                default:
                    $text = '';
                    break;
            }

            if ($text)
            {
                $time = gmdateModify('Y-m-d H:i:s', strtotime($item['time']), 0,$timezone,false); // 转换回该时区时间
                $day = date('Y-m-d', $time);

                if ($type === 1)
                {
                    $key = 0;
                } elseif ($type === 2)
                {
                    $key = 1;
                } else
                {
                    $key = $time;
                }
                $data[$day][$key] = [
                    'text' => $text
                ];
            }
        }

        $resData = [];
        foreach ($data as $day => $datum)
        {
            ksort($datum);

            $resData[] = [
                'date' => $day,
                'desc' => current($datum)['text']
            ];
        }

        return show(config('code.success'), '请求成功', $resData);
    }

    /**
     * 获取天象详情
     * @return \think\response\Json
     * @throws \Exception
     */
    public function getDayInfo()
    {
        $date = input('date/s', '');
        $timezone = input('timezone/f', 8);

        if (!$date)
        {
            return show(config('code.error'), '请求异常');
        }

        $intDate = (int)date('Ymd', strtotime($date));

        $astroChangeModel = new AstroChangeModel();
        // 获取星象变动数据 正在进行中的星象
        $underwayAstro = $astroChangeModel->getChangeDayList($date, $timezone);
        $astroChangeText = [];
        if ($this->user && $this->user['rid'])
        {
            $newIntDate = (int)date('Ymd'); // 今天的日期
             $isSendDingDing = $newIntDate === $intDate; // 是否发送钉钉消息

            $recodeInfo = (new Recode())->getInfoById($this->user['rid']);
            // 通过星象变动数据 获取星象变动词条
            $astroChangeText = $astroChangeModel->getChangeText($date, $timezone, $underwayAstro['listData'], $recodeInfo, $isSendDingDing);
        }

        if (!$astroChangeText)
        {   // 如果没有获取到星象变动词条 则使用 星座寄语 ->获取今天的星座寄语为词条
            $word = (new MiniDateText())->getWord($intDate, 1);
            if (trim($word))
            {
                $astroChangeText[] = [
                    'title' => '每日一语',
                    'content' => $word,
                ];
            }
        }
        // 获取今日天象推荐的商品
        $goods = (new ZlAstroCalendarGoods())->getGoodsToDate($intDate);

        $goodsData = [];
        foreach ($goods as $goodItem)
        {
            $goodsData[] = [
                'good_img' => $goodItem['img'],
                'img_scale' => $goodItem['img_scale'],
                'target' => $goodItem['target'],
                'link' => $goodItem['link'],
            ];
        }

        $resData = [
            'underway_astro' => $underwayAstro['resData'],
            'astro_info_list' => $astroChangeText,
            'goods_data' => $goodsData
        ];

        return show(config('code.success'), '请求成功', $resData);
    }
}
